package org.pjlib.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;

import androidx.appcompat.widget.AppCompatTextView;

import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.blankj.utilcode.util.SizeUtils;

import org.pjlib.R;

/**
 * Created by ws on 2019/04/08.
 */
public class ActionBar extends LinearLayout {
    private String text;
    private boolean isLeftBack;
    private LinearLayout left;
    private LinearLayout right;
    private LinearLayout center;
    private AppCompatTextView title;
    private int style;

    public ActionBar(Context context) {
        this(context, null);
    }

    public ActionBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ActionBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = LayoutInflater.from(context).inflate(R.layout.action_bar, null);
        view.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        setOrientation(VERTICAL);
        addView(view);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ActionBar, defStyleAttr, 0);
        text = a.getString(R.styleable.ActionBar_actionBar_text);
        isLeftBack = a.getBoolean(R.styleable.ActionBar_actionBar_left_back, false);
        style = a.getInteger(R.styleable.ActionBar_actionBar_style, 0);
        a.recycle();
    }

    public void setStyle(int style) {
        this.style = style;
        if (isLeftBack && getLeftView().getChildCount() > 0 && getLeftView().getChildAt(0) instanceof ImageView) {
            ImageView iv = (ImageView) getLeftView().getChildAt(0);
            iv.setImageResource(style == 0 ? R.drawable.ic_back_b : R.drawable.ic_back_d);
        }
        if(title!=null){
            if (style == 1) {
                title.setTextColor(Color.WHITE);
            }else {
                title.setTextColor(Color.BLACK);
            }
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        setLeftBack(isLeftBack);
        setTitle(text);
    }

    public ImageView setLeftBack(boolean isLeftBack) {
        if (isLeftBack) {
            ImageView iv_right = getImgView(style == 0 ? R.drawable.ic_back_b : R.drawable.ic_back_d);
            left = getLeftView();
            left.addView(iv_right);
            left.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (getContext() instanceof Activity) {
                        ((Activity) getContext()).finish();
                    }
                }
            });
            return iv_right;
        } else {
            left = getLeftView();
            left.removeAllViews();
            return null;
        }
    }

    public AppCompatTextView setTitle(String text) {
        if (!TextUtils.isEmpty(text)) {
            if (title == null) {
                center = getCenterView();
                title=center.findViewById(R.id.tv_title);
                if(title==null){
                    title = getCenterText(text);
                    center.addView(title);
                }
                if (style == 1) {
                    title.setTextColor(Color.WHITE);
                }else {
                    title.setTextColor(Color.BLACK);
                }
            }
            title.setText(text);
        }
        return title;
    }

    public AppCompatTextView getCenterText(String text) {
        AppCompatTextView tv = null;
        if (text != null && text.length() > 10) {
            tv = new ScrollForeverTextView(getContext());
        } else {
            tv = new AppCompatTextView(getContext());
            tv.setSingleLine();
            tv.setEllipsize(TextUtils.TruncateAt.END);
        }
        tv.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        tv.setMaxWidth(SizeUtils.dp2px( 250));

        if (!TextUtils.isEmpty(text)) {
            tv.setText(text);
        }
        tv.setTextColor(Color.BLACK);
        return tv;
    }

    public class ScrollForeverTextView extends AppCompatTextView {

        public ScrollForeverTextView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            setSingleLine();
            setEllipsize(TextUtils.TruncateAt.MARQUEE);
            setMarqueeRepeatLimit(-1);
        }

        public ScrollForeverTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            setSingleLine();
            setEllipsize(TextUtils.TruncateAt.MARQUEE);
            setMarqueeRepeatLimit(-1);
        }

        public ScrollForeverTextView(Context context, AttributeSet attrs,
                                     int defStyle) {
            super(context, attrs, defStyle);
            setSingleLine();
            setEllipsize(TextUtils.TruncateAt.MARQUEE);
            setMarqueeRepeatLimit(-1);
        }

        @Override
        public boolean isFocused() {
            return true;
        }

    }

    public AppCompatTextView getRightText(String text) {
        AppCompatTextView tv = new AppCompatTextView(getContext());
        tv.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        if (!TextUtils.isEmpty(text)) {
            tv.setText(text);
        }
        tv.setTextColor(Color.BLACK);
        tv.setTextSize(SizeUtils.dp2px(  5));
        return tv;
    }

    public ImageView getImgView(int resId) {
        ImageView iv = new ImageView(getContext());
        int wh = SizeUtils.dp2px(  30);
        int p = SizeUtils.dp2px(  5);
        iv.setLayoutParams(new LayoutParams(wh, wh));
        iv.setPadding(p, p, p, p);
        if (resId != 0) {
            iv.setImageResource(resId);
        }
        return iv;
    }

    public LinearLayout getLeftView() {
        if (left == null) {
            left = findViewById(R.id.ll_left);
        }
        return left;
    }

    public LinearLayout getRightView() {
        if (right == null) {
            right = findViewById(R.id.ll_right);
        }
        return right;
    }

    public LinearLayout getCenterView() {
        if (center == null) {
            center = findViewById(R.id.ll_center);
        }
        return center;
    }
}
